﻿#include"lv_bgcolor.h"
#include"lv_main_page.h"
static lv_obj_t* color_set_pannle;//颜色设置面板
static uint8_t red=255;//红色值
static uint8_t green=255;//绿色值
static uint8_t blue=255;//蓝色值
static uint8_t red_history =255;//历史值
static uint8_t green_history =255;//历史值
static uint8_t blue_history =255;//历史值
static lv_obj_t* red_slider;//红色调节滑块
static lv_obj_t* green_slider;//绿色调节滑块
static lv_obj_t* blue_slider;//蓝色调节滑块
static lv_obj_t* apply_btn;//应用按钮
static lv_obj_t* cancel_btn;//取消按钮
static void Main_page_color_set_btn_event_handler(lv_event_t* event);
static void color_slider_event_cb(lv_event_t* event);//颜色滑块事件处理函数
static void apply_or_cancel_btn_event_handler(lv_event_t* event);//应用按钮事件处理函数
/*主页背景颜色设置*/
void lv_main_bgcolor_set(void) {
    Main_page_color_set_btn = lv_btn_create(main_page);
    lv_obj_set_size(Main_page_color_set_btn, 150, 40);
    lv_obj_align(Main_page_color_set_btn, LV_ALIGN_TOP_LEFT,10,70);//左下对齐

    lv_obj_set_style_bg_color(Main_page_color_set_btn, lv_color_hex(0x007DFF), LV_STATE_DEFAULT);//设置默认状态为蓝色
    lv_obj_set_style_bg_color(Main_page_color_set_btn, lv_color_hex(0x0050A0), LV_STATE_PRESSED);//设置按下状态为淡蓝色
    lv_obj_add_event_cb(Main_page_color_set_btn, Main_page_color_set_btn_event_handler, LV_EVENT_CLICKED, NULL);//
    lv_obj_t* label = lv_label_create(Main_page_color_set_btn);
    lv_label_set_text(label, "Set main page color");
    lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);//居中显示

}
/*主页背景颜色设置按钮事件回调函数*/
static void Main_page_color_set_btn_event_handler(lv_event_t* event) {

    lv_obj_add_state(Main_page_color_set_btn, LV_STATE_PRESSED);//保持按下状态
    lv_obj_clear_flag(Main_page_color_set_btn, LV_OBJ_FLAG_CLICKABLE);//禁止点击
    //禁止主页被点击
    lv_obj_add_state(main_page, LV_STATE_DISABLED);
    lv_obj_clear_state(main_page, LV_STATE_DEFAULT);
    lv_obj_clear_flag(main_page, LV_OBJ_FLAG_CLICKABLE);


    //创建颜色设置面板
    color_set_pannle = lv_obj_create(main_page);
    lv_obj_set_size(color_set_pannle, 500, 300);
    lv_obj_align(color_set_pannle, LV_ALIGN_CENTER, 0, 0);
    //创建三个slider，分别为红绿蓝，用来调节颜色值
    red_slider = lv_slider_create(color_set_pannle);
    lv_obj_set_size(red_slider, 250, 30);
    lv_obj_align(red_slider, LV_ALIGN_TOP_MID, 20, 20);
    lv_slider_set_range(red_slider, 0, 255);//设置范围
    lv_slider_set_value(red_slider, red_history, LV_ANIM_OFF);//设置初始值, LV_ANIM_OFF);//设置初始值
    lv_obj_set_style_radius(red_slider, 90, LV_PART_MAIN | LV_STATE_DEFAULT);//设置圆角
    lv_obj_set_style_pad_left(red_slider, 15, LV_PART_MAIN | LV_STATE_DEFAULT);//设置左边距
    lv_obj_set_style_pad_right(red_slider, 15, LV_PART_MAIN | LV_STATE_DEFAULT);//设置右边距
    lv_obj_set_style_pad_top(red_slider, 5, LV_PART_MAIN | LV_STATE_DEFAULT);//设置上边距
    lv_obj_set_style_pad_bottom(red_slider, 5, LV_PART_MAIN | LV_STATE_DEFAULT);//设置下边距

    lv_obj_set_style_radius(red_slider, 90, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(red_slider, lv_color_hex(0xFF0030), LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(red_slider, 255, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_outline_width(red_slider, 0, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_outline_pad(red_slider, 0, LV_PART_INDICATOR | LV_STATE_DEFAULT);

    lv_obj_set_style_radius(red_slider, 90, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(red_slider, -15, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(red_slider, 0, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(red_slider, -5, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(red_slider, -5, LV_PART_KNOB | LV_STATE_DEFAULT);
    
    
    //创建红色标签
    lv_obj_t*red_lable=lv_label_create(color_set_pannle);
    lv_label_set_text(red_lable,"R");
    //设置字体颜色
    lv_obj_set_style_text_color(red_lable, lv_color_hex(0xFF0030), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_size(red_lable, 60, 30);
    lv_obj_align_to(red_lable, red_slider, LV_ALIGN_OUT_LEFT_MID, -10, 5);
    //创建红色值标签
    lv_obj_t* red_value_lable = lv_label_create(color_set_pannle);
    lv_obj_set_size(red_value_lable, 50, 30);
    lv_label_set_text_fmt(red_value_lable, "R:%d", red_history);
    lv_obj_align_to(red_value_lable, red_slider, LV_ALIGN_OUT_RIGHT_MID, 10, 5);
    lv_obj_set_style_text_color(red_value_lable, lv_color_hex(0xFF0030), LV_PART_MAIN | LV_STATE_DEFAULT);
    //添加红色值变化事件
    lv_obj_add_event_cb(red_slider, color_slider_event_cb, LV_EVENT_VALUE_CHANGED, red_value_lable);

    green_slider=lv_slider_create(color_set_pannle);
    lv_obj_set_size(green_slider, 250, 30);
    lv_obj_align(green_slider, LV_ALIGN_TOP_MID, 20, 70);
    lv_slider_set_range(green_slider, 0, 255);
    lv_slider_set_value(green_slider, green_history, LV_ANIM_OFF);
    lv_obj_set_style_radius(green_slider, 90, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(green_slider, 15, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(green_slider, 15, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(green_slider, 5, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(green_slider, 5, LV_PART_MAIN | LV_STATE_DEFAULT);

    lv_obj_set_style_radius(green_slider, 90, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(green_slider, lv_color_hex(0x00FF00), LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(green_slider, 255, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_outline_width(green_slider, 0, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_outline_pad(green_slider, 0, LV_PART_INDICATOR | LV_STATE_DEFAULT);

    lv_obj_set_style_radius(green_slider, 90, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(green_slider, -15, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(green_slider, 0, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(green_slider, -5, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(green_slider, -5, LV_PART_KNOB | LV_STATE_DEFAULT);

    lv_obj_t*green_lable=lv_label_create(color_set_pannle);
    lv_label_set_text(green_lable,"G");
    lv_obj_set_style_text_color(green_lable, lv_color_hex(0x00FF00), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_size(green_lable, 60, 30);
    lv_obj_align_to(green_lable, green_slider, LV_ALIGN_OUT_LEFT_MID, -10, 5);

    lv_obj_t* green_value_lable = lv_label_create(color_set_pannle);
    lv_obj_set_size(green_value_lable, 50, 30);
    lv_label_set_text_fmt(green_value_lable, "G:%d", green_history);
    lv_obj_align_to(green_value_lable, green_slider, LV_ALIGN_OUT_RIGHT_MID, 10, 5);
    lv_obj_set_style_text_color(green_value_lable, lv_color_hex(0x00FF00), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_add_event_cb(green_slider, color_slider_event_cb, LV_EVENT_VALUE_CHANGED, green_value_lable);

    blue_slider=lv_slider_create(color_set_pannle);
    lv_obj_set_size(blue_slider, 250, 30);
    lv_obj_align(blue_slider, LV_ALIGN_TOP_MID, 20, 120);
    lv_slider_set_range(blue_slider, 0, 255);
    lv_slider_set_value(blue_slider, blue_history, LV_ANIM_ON);
    lv_obj_set_style_radius(blue_slider, 90, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(blue_slider, 15, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(blue_slider, 15, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(blue_slider, 5, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(blue_slider, 5, LV_PART_MAIN | LV_STATE_DEFAULT);

    lv_obj_set_style_radius(blue_slider, 90, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(blue_slider, lv_color_hex(0x0000FF), LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(blue_slider, 255, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_outline_width(blue_slider, 0, LV_PART_INDICATOR | LV_STATE_DEFAULT);
    lv_obj_set_style_outline_pad(blue_slider, 0, LV_PART_INDICATOR | LV_STATE_DEFAULT);

    lv_obj_set_style_radius(blue_slider, 90, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(blue_slider, -15, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(blue_slider, 0, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(blue_slider, -5, LV_PART_KNOB | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(blue_slider, -5, LV_PART_KNOB | LV_STATE_DEFAULT);

    lv_obj_t*blue_lable=lv_label_create(color_set_pannle);
    lv_label_set_text(blue_lable,"B");
    lv_obj_set_style_text_color(blue_lable, lv_color_hex(0x0000FF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_size(blue_lable, 60, 30);
    lv_obj_align_to(blue_lable, blue_slider, LV_ALIGN_OUT_LEFT_MID, -10, 5);

    lv_obj_t* blue_value_lable = lv_label_create(color_set_pannle);
    lv_obj_set_size(blue_value_lable, 50, 30);
    lv_label_set_text_fmt(blue_value_lable, "B:%d", blue_history);
    lv_obj_align_to(blue_value_lable, blue_slider, LV_ALIGN_OUT_RIGHT_MID, 10, 5);
    lv_obj_set_style_text_color(blue_value_lable, lv_color_hex(0x0000FF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_add_event_cb(blue_slider, color_slider_event_cb, LV_EVENT_VALUE_CHANGED, blue_value_lable);
    //应用按钮
    apply_btn = lv_btn_create(color_set_pannle);
    lv_obj_set_size(apply_btn, 100, 40);
    lv_obj_align(apply_btn, LV_ALIGN_BOTTOM_MID, 0, -10);
    lv_obj_set_style_bg_color(apply_btn, lv_color_hex(0x007DFF), LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(apply_btn, lv_color_hex(0x0050A0), LV_STATE_PRESSED);
    lv_obj_t* apply_label = lv_label_create(apply_btn);
    lv_label_set_text(apply_label, "Apply");
    lv_obj_align(apply_label, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event_cb(apply_btn, apply_or_cancel_btn_event_handler, LV_EVENT_CLICKED, apply_btn);
    //取消按钮
    cancel_btn = lv_btn_create(color_set_pannle);
    lv_obj_set_size(cancel_btn, 100, 40);
    lv_obj_align(cancel_btn, LV_ALIGN_BOTTOM_MID, 0, -60);
    lv_obj_set_style_bg_color(cancel_btn, lv_color_hex(0xFF0000), LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(cancel_btn, lv_color_hex(0x900000), LV_STATE_PRESSED);
    lv_obj_t* cancel_label = lv_label_create(cancel_btn);
    lv_label_set_text(cancel_label, "Cancel");
    lv_obj_align(cancel_label, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event_cb(cancel_btn, apply_or_cancel_btn_event_handler, LV_EVENT_CLICKED, cancel_btn);

    lv_color_t color_value = lv_color_make(red_history, green_history, blue_history);//获取颜色
    lv_obj_set_style_bg_color(main_page, color_value, LV_STATE_DEFAULT);//设置颜色



    lv_obj_t* ui_Label5 = lv_label_create(color_set_pannle);
    lv_obj_set_width(ui_Label5, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_Label5, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_align(ui_Label5, LV_ALIGN_BOTTOM_LEFT);
    lv_label_set_recolor(ui_Label5, "true");
    lv_obj_set_style_text_color(ui_Label5, lv_color_hex(0xFF0041), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_Label5, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_letter_space(ui_Label5, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui_Label5, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_Label5, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_Label5, lv_color_hex(0x0000FF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_Label5, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
}
/*
颜色滑块事件回调函数
滑块滑动时会将值存入相应的颜色变量中，并实时显示效果
 */
static void color_slider_event_cb(lv_event_t* event)
{
    lv_obj_t* ta_slider = lv_event_get_target(event);
    lv_event_code_t code = lv_event_get_code(event);
    lv_obj_t* value_label = (lv_obj_t*)lv_event_get_user_data(event);

    if (event->target == red_slider) {
        lv_label_set_text_fmt(value_label, "R:%d", lv_slider_get_value(ta_slider));//显示颜色值
        red = lv_slider_get_value(ta_slider);
    }
    else if (event->target ==green_slider){
        lv_label_set_text_fmt(value_label, "G:%d", lv_slider_get_value(ta_slider));
        green = lv_slider_get_value(ta_slider);
    }
    else if (event->target ==blue_slider){
        lv_label_set_text_fmt(value_label, "B:%d", lv_slider_get_value(ta_slider));
        blue = lv_slider_get_value(ta_slider);
    }
    //显示实时效果
    lv_color_t color_value = lv_color_make(red, green, blue);
    lv_obj_set_style_bg_color(main_page, color_value, LV_STATE_DEFAULT);
    
}
/*
应用按钮事件回调函数
 */
void apply_or_cancel_btn_event_handler(lv_event_t* event){
    lv_obj_t* btn = lv_event_get_target(event);
    lv_event_code_t code = lv_event_get_code(event);
    
    if (event->target == apply_btn) {
        //保存颜色值到历史记录
        red_history   = red;
        green_history = green;
        blue_history  = blue;

    }
    else if (event->target == cancel_btn) {//如果点了取消则，恢复历史记录的颜色值，否则下次进入点击应用就会是上次调整时的颜色值
        //取消颜色设置
        red=red_history;
        green=green_history;
        blue=blue_history;
        }
    lv_obj_del(color_set_pannle);
    lv_obj_clear_state(Main_page_color_set_btn, LV_STATE_PRESSED);//保持按下状态
    lv_obj_add_state(Main_page_color_set_btn, LV_STATE_DEFAULT);//恢复默认状态
    lv_obj_add_flag(Main_page_color_set_btn, LV_OBJ_FLAG_CLICKABLE);//恢复可点击状态

    lv_obj_add_flag(main_page, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_state(main_page, LV_STATE_DEFAULT);
    lv_obj_clear_state(main_page, LV_STATE_DISABLED);

    lv_color_t color_value = lv_color_make(red_history, green_history, blue_history);
    lv_obj_set_style_bg_color(main_page, color_value, LV_STATE_DEFAULT);
}
